package com.my.hotel.service.impl;

import com.my.hotel.controller.serverPage.UtilsController;
import com.my.hotel.entity.serverPage.JdUser;
import com.my.hotel.mapper.JdUserMapper;
import com.my.hotel.service.JdUserService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
@Service
public class JdUserServiceImpl implements JdUserService {
    @Autowired
    JdUserMapper mapper;
    @Override
    public List<JdUser> getList() {
        return mapper.getList();
    }

    @Override
//    @Transactional
    public void register(JdUser user, HttpSession session, String varifyCode, HttpServletRequest req) {
        mapper.register(user);
        setSession(user,session,varifyCode,req);

    }

    public void setSession(JdUser user, HttpSession session,String varifyCode, HttpServletRequest req){
        session.removeAttribute("validateCode");
        session.setAttribute("userInfo",user);
    }
    public JdUser setSession(HttpSession session){
        return (JdUser)session.getAttribute("userInfo");
    }


    @Override
    public void delClient(int delId) {
    mapper.delClient(delId);
    }

    @Override
    public JdUser getClientById(int id) {
        return mapper.getClientById(id);
    }

    @Override
    public void clientUpdate(JdUser user) {
        mapper.clientUpdate(user);
    }

    @Override
    public boolean isLogin(String dl_username, String dl_password,HttpSession session) {
        JdUser login = mapper.isLogin(dl_username, dl_password);
        if(ObjectUtils.isNotEmpty(login)){
            session.removeAttribute("loginmsg");
            session.setAttribute("userInfo",login);
            return true;
        } else
                session.setAttribute("loginmsg","账号或密码有误");
        return false;
    }

    @Override
    public int getCountByUsername(String username) {
      return   mapper.getCountByUsername( username);

    }

    @Override
    public JdUser cklogin(String username) {
        return mapper.cklogin(username);
    }
}
